package com.macken.wow.dao;

import java.util.List;

import org.springframework.jdbc.core.SingleColumnRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;

import com.macken.wow.entity.UserInfo;
import com.macken.wow.util.DaoSupport;

public class UserDao {
	public static final UserDao instance = new UserDao();
	private UserDao() {

	}
	public boolean insert(UserInfo user) {
		String sql = "insert into user_info(user_name,user_pwd,user_email,create_time,create_ip,verify_code)values(:userName,:userPwd,:userEmail,:createTime,:createIp,:verifyCode)";
		int row = DaoSupport.db.getNamedParameterJdbcOperations().update(sql, new BeanPropertySqlParameterSource(user));
		return row > 0;
	}
	public UserInfo getUserById(Long uid) {
		String sql = "select * from user_info where uid=?";
		List<UserInfo> list = DaoSupport.db.query(sql, UserInfo.rowMapper, uid);
		if (list.size() > 0) {
			return list.get(0);
		}
		return null;
	}
	public UserInfo getUserByName(String name) {
		String sql = "select * from user_info where user_name=?";
		List<UserInfo> list = DaoSupport.db.query(sql, UserInfo.rowMapper, name);
		if (list.size() > 0) {
			return list.get(0);
		}
		return null;
	}
	public UserInfo getUserByVerifyEmail(String email) {
		String sql = "select * from user_info where user_email=? and email_verify=1";
		List<UserInfo> list = DaoSupport.db.query(sql, UserInfo.rowMapper, email);
		if (list.size() > 0) {
			return list.get(0);
		}
		return null;
	}
	public UserInfo getUserByEmailCode(String email, String code) {
		String sql = "select * from user_info where user_email=? and verify_code=?";
		List<UserInfo> list = DaoSupport.db.query(sql, UserInfo.rowMapper, email, code);
		if (list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	public boolean insertRealMd(String userName, String pass, String email) {
		String sql = "insert into account(username,sha_pass_hash,email,expansion) values(?,?,?,1)";
		int rows = DaoSupport.realDb.update(sql, userName, pass, email);
		return rows > 0;
	}

	public int getWowId(String userName) {
		String sql = "select id from account where username=?";
		List<Integer> res = DaoSupport.realDb.query(sql, new SingleColumnRowMapper<Integer>(Integer.class), userName);
		if (res.size() > 0) {
			return res.get(0);
		}
		return -1;
	}
	public boolean updateUserVerify(UserInfo user) {
		String sql = "update user_info set email_verify=? where uid=?";
		int rows = DaoSupport.db.update(sql, user.emailVerify, user.uid);
		return rows > 0;
	}
	public boolean updateUserWowId(int wowId, String uid) {
		String sql = "update user_info set wow_id=? where user_name=?";
		int rows = DaoSupport.db.update(sql, wowId, uid);
		return rows > 0;
	}
	public boolean updateUserPwd(UserInfo user) {
		String sql = "update user_info set user_pwd=? where uid=?";
		int rows = DaoSupport.db.update(sql, user.userPwd, user.uid);
		return rows > 0;
	}
	public boolean updateReamlPwd(UserInfo user, String pass) {
		String sql = "update account set sha_pass_hash=? where username=?";
		int rows = DaoSupport.realDb.update(sql, pass, user.userName);
		return rows > 0;
	}
	public static void main(String[] args) {
		UserDao dao = UserDao.instance;
//		dao.insertRealMd("macken123", "123");
	}
}
